define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/op/models'),
        utils = require('js/utils/tools'),
        app = Backbone.history;
    require('datetimepicker');

    var FeedbackListView = Backbone.View.extend({
        tagName: "div",
        events: {
            'click #search': 'doSearch',
            'click #export': 'export',
        },
        initialize: function (options) {
            this.options = options;
            this.collection = new models.FeedbackCollection();
            this.collection.bind('change reset remove', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            return this;
        },
        _getSearchOption: function () {
            var options = utils.getAllInput("#sidebar_right", true),
                searched = utils.getAllInput('#sidebar_right');
            utils.saveSearched('op/feedback', searched);
            return options;
        },
        doSearch: function () {
            var options = this._getSearchOption();
            app.navigate(utils.composeQueryString('#op/feedback/', options), {
                trigger: true
            });
        },
        export: function () {
            notify.warning('警告', '导出中，请等待...');
            var options = this._getSearchOption();
            utils.exportData(this.collection.url, options);
            return false;
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                searched: utils.getSearched('op/feedback')
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || '-created_at',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#op/feedback/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            $('.date-input').datetimepicker({
                format: 'YYYY-MM-DD',
            });
            utils.getPaginator(that.options, this.collection.total, '#op/feedback/');
            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "op/FeedbackList.html",

        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            var view = new FeedbackListView(this.options);
            view.template = this.template;

            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });

});